package com.asiainfo.hybridapp.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.RelativeLayout;

import com.asiainfo.hybridapp.R;
import com.asiainfo.hybridapp.configs.ConstantsME;
import com.asiainfo.hybridapp.configs.EventBusTag;
import com.asiainfo.hybridapp.configs.InterfaceConfig;
import com.asiainfo.hybridapp.entity.PopulationRankInfo;
import com.asiainfo.hybridapp.fragment.CommonWebviewFragment;
import com.framework.utils.ScreenUtils;
import com.framework2.customviews.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import custom.org.greenrobot.eventbus.Subscribe;
import custom.org.greenrobot.eventbus.ThreadMode;

/**
 * 监控详情
 *
 * @author YobertJomi
 * className MonitorDetailActivity
 * created at  2017/6/16  11:31
 */
public class MonitorDetailActivity extends BaseActivity {

    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.relative_center_homepage)
    RelativeLayout relativeCenterHomepage;

    private Fragment lastShownFragment;
    private Fragment fragment_1;
    private Fragment fragment_2;
    private Fragment fragment_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_monitor_detail);
        ButterKnife.bind(this);
        ScreenUtils.getInstance().setTranslucentStatus(this, true);
        ScreenUtils.getInstance().setStatusBarTintColor(this,
                getResources().getColor(R.color.white));
        initView();
        showFragment(0);
    }

    //eventBus通知新消息
    @Subscribe(threadMode = ThreadMode.MAIN, tag = EventBusTag.newMessage)
    public void eventBusNotify(String info) {
    }

    private void initView() {
        try {
            if (getIntent().getSerializableExtra(ConstantsME.entity) != null) {
                PopulationRankInfo info = (PopulationRankInfo) getIntent().getSerializableExtra(ConstantsME.entity);
                if (null != info) {
                    titleView.setTitle(info.getTitle());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tabLayout.addTab(tabLayout.newTab().setText("基本信息"));
        tabLayout.addTab(tabLayout.newTab().setText("当前人群结构分析"));
        tabLayout.addTab(tabLayout.newTab().setText("外地人口结构分析"));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = 0;
                if (TextUtils.equals("基本信息", tab.getText())) {
                    position = 0;
                } else if (TextUtils.equals("当前人群结构分析", tab.getText())) {
                    position = 1;
                } else if (TextUtils.equals("外地人口结构分析", tab.getText())) {
                    position = 2;
                }
                showFragment(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void showFragment(int position) {
        Fragment Center_mContent = null;
        FragmentTransaction ft = getSupportFragmentManager()
                .beginTransaction();
        if (position == 0 && fragment_1 == null) {
            fragment_1 = new CommonWebviewFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putString(ConstantsME.url, InterfaceConfig.link3);
            fragment_1.setArguments(bundle1);
            ft.add(R.id.relative_center_homepage, fragment_1);
        } else if (position == 1 && fragment_2 == null) {
            fragment_2 = new CommonWebviewFragment();
            Bundle bundle2 = new Bundle();
            bundle2.putString(ConstantsME.url, InterfaceConfig.link1);
            fragment_2.setArguments(bundle2);
            ft.add(R.id.relative_center_homepage, fragment_2);
        } else if (position == 2 && fragment_3 == null) {
            fragment_3 = new CommonWebviewFragment();
            Bundle bundle3 = new Bundle();
            bundle3.putString(ConstantsME.url, InterfaceConfig.link2);
            fragment_3.setArguments(bundle3);
            ft.add(R.id.relative_center_homepage, fragment_3);
        }
        if (position == 0) {
            Center_mContent = fragment_1;
        } else if (position == 1) {
            Center_mContent = fragment_2;
        } else if (position == 2) {
            Center_mContent = fragment_3;
        }
        ScreenUtils.getInstance().setTranslucentStatus(this, true);
        ScreenUtils.getInstance().setStatusBarTintColor(this, getResources().getColor(R.color.white));
        if (lastShownFragment != null) {
            ft.hide(lastShownFragment).show(Center_mContent).commit();
        } else {
            ft.show(fragment_1).commit();
        }
        lastShownFragment = Center_mContent;
    }
}
